package com.coding.kan_yi_dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.coding.kan_yi_dao.common.Result;
import com.coding.kan_yi_dao.entity.AdminUser;
import com.coding.kan_yi_dao.mapper.AdminUserMapper;
import com.coding.kan_yi_dao.service.IAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.hutool.crypto.digest.DigestUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 砍一刀，不买就砍你一刀
 * @since 2023-09-20
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {
    @Resource
    AdminUserMapper adminUserMapper;

    @Override
    public Result gologin(AdminUser adminUser) {
        String md5password = new String(DigestUtil.md5Hex(adminUser.getAdminPassword()));
        AdminUser adminUser1 = query().eq("admin_name", adminUser.getAdminName())
                .eq("admin_password", md5password).one();
       /* AdminUser adminUser2 = new AdminUser();
        adminUser2.setAdminId(adminUser1.getAdminId());
        adminUser2.setAdminName(adminUser1.getAdminName());*/
        if (adminUser1 == null) {
            return Result.fail("账号或密码错误");
        }
        return Result.ok("成功", adminUser1);
    }

    @Override
    public Result go_update_password(Integer adminId, String adminPassword, String new_adminPassword) {
        String md5password = new String(DigestUtil.md5Hex(adminPassword));
        AdminUser adminUser1 = query().eq("admin_id", adminId).one();
        if (adminUser1.getAdminPassword().equals(md5password)) {
            String new_md5password = new String(DigestUtil.md5Hex(new_adminPassword));
            AdminUser adminUser = new AdminUser();
            adminUser.setAdminId(adminId);
            adminUser.setAdminPassword(new_md5password);
            Boolean a = updateById(adminUser);
            return Result.ok(a);
        }
        return Result.fail("原密码错误");
        /* QueryWrapper<AdminUser> qw = new QueryWrapper<>();
            qw.eq("admin_id", adminId);
            Integer b = adminUserMapper.update(adminUser,qw);*/
    }

    @Override
    public Result gotest() {
        List<AdminUser> emps = adminUserMapper.selectList(null);

        return Result.ok(emps);
    }
}
